﻿using System.Diagnostics;
using System.Linq;
using AntDesignProCore.MVC.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AntDesignProCore.MVC.Controllers
{
  public class ArticleController : BaseController
  {
    ResponseModel json = new ResponseModel();

    public ArticleController(MariaDBContext context) : base(context)
    {

    }

    //[ServiceFilter(typeof(ActionLoggingFilter))]
    public async Task<IActionResult> LoadArticleList(int current, int pageSize, Article article)
    {
      var count = await _db.Articles.CountAsync();

      bool b1 = article.id > 0;
      bool b2 = !string.IsNullOrEmpty(article.article_title);
      bool b3 = !string.IsNullOrEmpty(article.article_content);

      var list = await _db.Articles
          .WhereIf(a => a.id == article.id, article.id > 0)
          .WhereIf(a => a.article_title == article.article_title, !string.IsNullOrEmpty(article.article_title))
          .WhereIf(a => a.article_content == article.article_content, !string.IsNullOrEmpty(article.article_content))
          .OrderByDescending(a => a.id)
          .Skip((current - 1) * pageSize)
          .Take(pageSize)
          .ToListAsync();

      json.success = true;
      json.data = list;
      json.total = count;
      json.current = current;
      json.pageSize = pageSize;

      return Json(json);
    }

    //[ServiceFilter(typeof(ActionLoggingFilter))]
    public IActionResult MoreActionArticle()
    {
      //新增多条
      List<Article> list = new List<Article>();
      for (int i = 0; i < 5; i++)
      {
        list.Add(new Article { article_title = "add" + i, article_content = "add" + i, create_date = DateTime.Now });
      }
      _db.AddRangeAsync(list);

      //修改多条
      List<Article> updateList = _db.Articles.OrderBy(a => a.id).Take(3).ToList();
      foreach (Article article in updateList)
      {
        article.article_title = "update" + Guid.NewGuid().ToString();
      }


      //删除多条
      List<Article> deleteList = _db.Articles.OrderByDescending(a => a.id).Take(3).ToList();
      _db.RemoveRange(deleteList);

      bool result = _db.SaveChanges() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "添加成功！";
      }
      else
      {
        json.msg = "添加失败！";
      }

      return Json(json);
    }

    public async Task<IActionResult> AddArticle([FromBody] Article article)
    {
      article.create_date = DateTime.Now;
      article.modify_date = DateTime.Now;
      await _db.Articles.AddAsync(article);
      bool result = await _db.SaveChangesAsync() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "添加成功！";
      }
      else
      {
        json.msg = "添加失败！";
      }

      return Json(json);
    }

    public async Task<IActionResult> UpdateArticle([FromBody] Article viewModel)
    {
      var dbModel = await _db.Articles.Where(a => a.id == viewModel.id).FirstOrDefaultAsync();

      if (dbModel == null)
      {
        json.msg = "实体不存在！";
        return Json(json);
      }

      dbModel.article_title = viewModel.article_title;
      dbModel.article_content = viewModel.article_content;
      dbModel.modify_date = DateTime.Now;

      bool result = await _db.SaveChangesAsync() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "修改成功！";
      }
      else
      {
        json.msg = "修改失败！";
      }

      return Json(json);
    }

    public async Task<IActionResult> DeleteArticle(string ids)
    {
      var idList = ids.Split(',').AsEnumerable();

      var delList = _db.Articles.Where(a => idList.Contains(a.id.ToString()));

      _db.Articles.RemoveRange(delList);

      bool result = await _db.SaveChangesAsync() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "删除成功！";
      }
      else
      {
        json.msg = "删除失败！";
      }

      return Json(json);
    }
  }
}
